package com.zxh.dao;

import java.io.IOException;
import java.io.InputStream;

import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static String driver ;
    private static String username;
    private static String url;
    private static String password;

    // 静态模块。类加载时就初始化
    static {
        // 通过类加载器读取对应的资源
        InputStream ins = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();

        try {
            properties.load(ins);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        driver = properties.getProperty("driver");
        username = properties.getProperty("username");
        url = properties.getProperty("url");
        password = properties.getProperty("password");

        properties.list(System.out);
        System.out.println("读取数据库信息如下：");
        System.out.println("driver=" + driver);
        System.out.println("url=" + url);
        System.out.println("username=" + username);
        System.out.println("password=" + password);
    }

    // 获取数据库的链接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
        }
        return connection;
    }

    // 编写查询公共类
    public static ResultSet execute(Connection con, PreparedStatement preparedStatement, ResultSet rs, String sql, Object[] params) throws SQLException {
        // 预编译的sql，在后面直接执行就行了
            preparedStatement = con.prepareStatement(sql);
            for (int i = 0; i < params.length; i++){
                preparedStatement.setObject(i+1, params[i]);
            }
            rs = preparedStatement.executeQuery();
            return rs;

    }
//    编写更新公共类

    public static int execute(Connection con, PreparedStatement preparedStatement, String sql, Object[] params) throws SQLException {
        // 预编译的sql，在后面直接执行就行了
        preparedStatement = con.prepareStatement(sql);
        for (int i = 0; i < params.length; i++){
            preparedStatement.setObject(i+1, params[i]);
        }
        return  preparedStatement.executeUpdate();
    }

    public static boolean closeResult(Connection con, PreparedStatement preparedStatement, ResultSet resultSet){
        boolean flag = true;
        if (resultSet != null){
            try {
                resultSet.close();
                resultSet = null;
            } catch (SQLException e) {
                flag = false;
                throw new RuntimeException(e);

            }
        }

        if (preparedStatement != null){
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException e) {
                flag = false;
                throw new RuntimeException(e);

            }
        }

        if (con != null){
            try {
                con.close();
                con = null;
            } catch (SQLException e) {
                flag = false;
                throw new RuntimeException(e);

            }
        }
        return flag;

    }





}

